package com.litbw.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

import com.litbw.event.Event;

public class EventCellRenderer extends JPanel
		implements ListCellRenderer {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 474205392834800300L;

	/**
	 * Image to use next to info.
	 */
	private static ImageIcon sun_img = null;
	
	/**
	 * The width of the outer panel.
	 */
	private static final int WIDTH = 300;
	/**
	 * The height of the outer panel.
	 */
	private static final int HEIGHT = 100;
	
	/**
	 * The width of the inner panel.
	 */
	private static final int INNER_WIDTH = 270;
	/**
	 * The height of the outer panel.
	 */
	private static final int INNER_HEIGHT = 70;
	
	/**
	 * The Inner panel, used to house all the visible elements.
	 */
	private JPanel innerPanel;
	/**
	 * The info panel, used to house the 3 rows of data.
	 */
	private JPanel info_panel;
	/**
	 * The description label.
	 */
	private JLabel desc_lbl;
	/**
	 * The location label.
	 */
	private JLabel loc_lbl;
	/**
	 * The date label.
	 */
	private JLabel date_lbl;
	/**
	 * The Image label.
	 */
	private JLabel img_lbl;
	
	/**
	 * Create a new instance of the EventCellRenderer.
	 */
	public EventCellRenderer() {
		
		// Try and load the image for the first time.
		if (sun_img == null) {
			try {
				sun_img = new ImageIcon(ImageIO.read(getClass().getResource("images/sun.png")));
			} catch (IOException ioe) { ioe.printStackTrace();}
		}

		// Setup this jpanels properties.
		setOpaque(true);
		setLayout(new BorderLayout());
		setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
		setBackground(new Color(0, 0, 0, 0));
		
		final Dimension dim = new Dimension(WIDTH, HEIGHT);
		setMaximumSize(dim);
		setMinimumSize(dim);
		setPreferredSize(dim);
		
		// Create inner panels.
		innerPanel = new JPanel(new BorderLayout());		
		info_panel = new JPanel(new GridLayout(3, 1));

		// Setup dimensions of inner panel so they are all the same.
		final Dimension innerDim = new Dimension(INNER_WIDTH, INNER_HEIGHT);
		innerPanel.setMaximumSize(innerDim);
		innerPanel.setMinimumSize(innerDim);
		innerPanel.setPreferredSize(innerDim);
		
		// Create borders.
		info_panel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.black));
		innerPanel.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		
		// Add components to the cell.
		loc_lbl = new JLabel();
		info_panel.add(loc_lbl);
		date_lbl = new JLabel();
		info_panel.add(date_lbl);
		desc_lbl = new JLabel();
		info_panel.add(desc_lbl);
		innerPanel.add(info_panel, BorderLayout.CENTER);
		add(innerPanel, BorderLayout.CENTER);

		// Set image if we can.
		if (sun_img != null) {
			img_lbl = new JLabel();
			img_lbl.setIcon(sun_img);
			innerPanel.add(img_lbl, BorderLayout.WEST);
		}
		
	}
	
	@Override
	public Component getListCellRendererComponent(JList list,
												  Object value,
												  int index,
												  boolean isSelected,
												  boolean cellHasFocus) {
		
		desc_lbl.setText(" Description: " + ((Event)value).getDescription());
		date_lbl.setText(" Date: " + ((Event)value).getDate());
		loc_lbl.setText(" Location: " + ((Event)value).getDescription());
		
		Color background;
		Color foreground;
		Color infobg;
		
        // check if this cell is selected
        if (isSelected) {
            background = Color.RED;
            foreground = Color.WHITE;
            infobg = new Color(200, 200, 255, 150);

        // unselected
        } else {
            background = Color.WHITE;
            foreground = Color.BLACK;
            infobg = Color.LIGHT_GRAY;
        };
		
        innerPanel.setBackground(background);
        innerPanel.setForeground(foreground);
        info_panel.setBackground(infobg);
        
		return this;
	}

}
